package com.demo.employees.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;

import com.demo.employees.service.EmployeeService;
import com.demo.employees.util.SimpleFactory;

/**
 * Servlet implementation class DelEmployee
 */
public class DelEmployeeServlet extends HttpServlet {
	private EmployeeService employeeService = SimpleFactory.getEmployeeService();
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DelEmployeeServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		try {
			if (StringUtils.isNotBlank(idStr)) {
				Long id = Long.parseLong(idStr);
				employeeService.deleteById(id);
				response.sendRedirect(request.getContextPath()+"/ListEmployee");
			}
		} catch (Exception e) {
			request.setAttribute("errMsg", "未知错误");
			request.getRequestDispatcher("error.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
